package com.yaoxiaowen.project2.test;

import android.annotation.TargetApi;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yaoxiaowen.project2.R;
import com.yaoxiaowen.project2.base.BaseActivity;
import com.yaoxiaowen.project2.test.javascript.AndroidCommonJsInterface;
import com.yaoxiaowen.project2.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class CommonWebViewActivity extends BaseActivity {


    @BindView(R.id.commonWebView)
    WebView commonWebView;

    public static final String TAG = "TestJavascriptMainActivity";
    private static final boolean DEBUG = true;
    private static final boolean IS_SHOW = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comma_web_view;
    }

    @Override
    protected void initData() {

    }


    @TargetApi(19)
    @Override
    protected void initView() {
        WebSettings webSettings = commonWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");

        commonWebView.addJavascriptInterface(new AndroidCommonJsInterface(), "AndroidCommonJsInterface");

        commonWebView.loadUrl("file:///android_asset/hello.html");
        //这句话让其显示 Alert
        commonWebView.setWebChromeClient(new WebChromeClient(){});


        commonWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                commonWebView.loadUrl("javascript:callFormJava('java 调用 js')");

                commonWebView.evaluateJavascript("getGreetings(yaowen)", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        ToastUtils.simpleShow(getBaseContext(), value);
                        mLog.i(DEBUG, TAG, "value = " + value);
                    }
                });
            }
        });
    }

    @Override
    protected void initListener() {

    }


    public void javaCallJs(){

    }

    @OnClick(R.id.commonWVBtn)
    public void btnOnClick(View view){
        javaCallJs();
    }
}
